package com.clg.protobuf.controller;

import com.clg.protobuf.entity.User;
import com.clg.protobuf.proto.UserRequestDTO.UserDTO;
import com.clg.protobuf.util.TestUtil;
import com.googlecode.protobuf.format.JsonFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 测试 Protocol Buffer
 * Created by chenlong on 2017/5/26.
 */
@RestController
public class ProtobufController {

	@Autowired
	private TestUtil testUtil;

	private final Logger logger = LoggerFactory.getLogger(ProtobufController.class);

	/**
	 * 根据用户ID查询用户信息
	 * @param idNo
	 * @return
	 */
	@GetMapping("/user/{idNo}")
	public UserDTO findUserById(@PathVariable("idNo") Integer idNo) {
		UserDTO userDTO = testUtil.getUserDTO(idNo);
		logger.info(userDTO.toString());
		return userDTO;
	}

	/**
	 * 根据用户ID查询用户信息
	 * @param idNo
	 * @return
	 */
	@GetMapping("/findUser/{idNo}")
	public User findUser(@PathVariable("idNo") Integer idNo) {
		return testUtil.getUser(idNo);
	}

	/**
	 * 根据用户ID查询用户信息
	 * @param userDTO
	 * @return
	 */
	@PostMapping("/addUser")
	public UserDTO addUser(@RequestBody UserDTO userDTO) {
		logger.info("==================== Protocol Buffer Object: " + userDTO);
		JsonFormat jsonFormat = new JsonFormat();
		String jsonData = jsonFormat.printToString(userDTO);
		logger.info("==================== JSON Object: " + jsonData);
		User user = testUtil.convert(userDTO);
		logger.info("==================== Entity Object: " + user);
		return userDTO;
	}
}
